package com.tarena.lbs.basic.web.controller;

import com.tarena.lbs.base.protocol.model.Result;

import com.tarena.lbs.base.protocol.pager.BasePageList;
import com.tarena.lbs.base.protocol.pager.PageResult;
import com.tarena.lbs.basic.web.service.IAdminService;
import com.tarena.lbs.common.basic.converter.AdminConverter;
import com.tarena.lbs.pojo.basic.bo.AdminBO;
import com.tarena.lbs.pojo.basic.param.AdminParam;
import com.tarena.lbs.pojo.basic.query.AdminQuery;
import com.tarena.lbs.pojo.basic.vo.AdminVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import javax.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
@Api(tags = "账户管理")
public class AdminController {

    @Autowired
    private IAdminService adminService;

    @Resource
    private AdminConverter adminConverter;


    @PostMapping("/admin/basic/role/add")
    @ApiOperation("新增用户")
    public Result AdminAdd(@RequestBody AdminParam adminParam) {
        //TD 新增优化
        adminService.insert(adminParam);
        return new Result<>("新增成功");
    }

    @DeleteMapping("/admin/basic/role/delete")
    @ApiOperation("删除用户")
    @ApiImplicitParams({
        @ApiImplicitParam(value = "用户id", name = "id", example = "0", required = true),
    })
    public Result AdminDelete(Integer id) {
        adminService.deleteById(id);
        return new Result<>("删除成功");
    }

    @PutMapping("/admin/basic/role/resetPassword")
    @ApiOperation("重置密码")
    @ApiImplicitParams({
        @ApiImplicitParam(value = "用户id", name = "id", example = "0", required = true),
    })
    public Result resetPassword(Integer id) {
        adminService.resetPasswordById(id);
        return new Result<>("重置成功");
    }

    @PutMapping("/admin/basic/role/assignRoles")
    @ApiOperation("分配角色")
    @ApiImplicitParams({
        @ApiImplicitParam(value = "用户id", name = "id", example = "0", required = true),
        @ApiImplicitParam(value = "角色", name = "accountType", example = "0", required = true),
    })
    public Result assignRoles(Integer id,Integer accountType) {
        adminService.assignRoles(id,accountType);
        return new Result<>("分配成功");
    }

    @PostMapping("/admin/basic/role/list")
    @ApiOperation("获取用户列表-分页")
    public Result<PageResult<AdminVO>> getAll(@RequestBody AdminQuery adminQuery) {
        BasePageList<AdminBO> adminBoList = adminService.userList(adminQuery);
        PageResult<AdminVO> page = adminConverter.assemblePageVOS(adminBoList,adminQuery);
        return new Result<>(page);
    }

    @GetMapping("admin/basic/role/detail")
    @ApiOperation("后台:用户详情")
    public Result<AdminVO> userDetail() {
        AdminBO adminBO = adminService.getAdminById(null);
        return new Result<>(adminConverter.bo2vo(adminBO));
    }
}
